package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
)

func main() {
	//启动udp拨号
	socket, err := net.DialUDP("udp", nil, &net.UDPAddr{
		IP:   net.IPv4(127, 0, 0, 1),
		Port: 40000,
	})
	if err != nil {
		fmt.Println("client conn failed", err)
		return
	}
	var reply [4]byte
	//var reply = make([]byte, 4)
	reader := bufio.NewReader(os.Stdin) //获取命令行输入
	for {
		fmt.Print("请输入：")
		msg, _ := reader.ReadString('\n')
		socket.Write([]byte(msg))

		//获取服务端返回的消息
		n, addr, err := socket.ReadFromUDP(reply[:])
		if err != nil {
			fmt.Println("read failed err:", err)
			continue
		}
		fmt.Printf("收到 addr:%v 回复消息：%s", addr, string(reply[0:n]))
	}
}
